<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="generator" content="PHPDoctor 2RC4 (http://peej.github.com/phpdoctor/)">
<meta name="when" content="Mon, 02 May 2011 02:46:56 +0000">

<link rel="stylesheet" type="text/css" href="../stylesheet.css">
<link rel="start" href="../overview-summary.html">

<title>request.php (Cornice)</title>

</head>
<body id="file" onload="parent.document.title=document.title;">

<div class="header">
<h1>Cornice</h1>
<ul>
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li><li>Class</li><li>Tree</li><li><a href="../overview-files.html">Files</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li><a href="../todo-list.html">Todo</a></li>
<li><a href="../index-all.html">Index</a></li>
</ul>
</div>

<div class="small_links">
<a href="../index.html" target="_top">Frames</a>
<a href="../source\request.php.html" target="_top">No frames</a>
</div>
<hr>

<h1>request.php</h1>
<div class="comment" id="overview_description"><p>============================================================================
Cornice PHP Framework & Website Builder
Freeware (f) 2011 by Inforo Software & Computer
============================================================================</p></div>

<hr>

<a name="line1"></a><pre><?php
<a name="line2"></a>/**
<a name="line3"></a> *============================================================================
<a name="line4"></a> * Cornice PHP Framework & Website Builder
<a name="line5"></a> * Freeware (f) 2011 by Inforo Software & Computer
<a name="line6"></a> *============================================================================
<a name="line7"></a> */
<a name="line8"></a> 
<a name="line9"></a>/**
<a name="line10"></a> * Class processing HTTP request
<a name="line11"></a> * @author Nicola Dinh
<a name="line12"></a> * @version 2011.04.2217
<a name="line13"></a> */
<a name="line14"></a>class request {
<a name="line15"></a>
<a name="line16"></a>  /*
<a name="line17"></a>   * All properties
<a name="line18"></a>   */
<a name="line19"></a>  private static $module = null;
<a name="line20"></a>  private static $page = null;
<a name="line21"></a>  private static $validated_url = null;
<a name="line22"></a>  private static $url_params = array();
<a name="line23"></a>  private static $form_params = array();
<a name="line24"></a>
<a name="line25"></a>  /**
<a name="line26"></a>   * Default instance level constructor
<a name="line27"></a>   * Just decorative, should not be called due to having only static methods
<a name="line28"></a>   */
<a name="line29"></a>  public function __construct() {
<a name="line30"></a>    //
<a name="line31"></a>  }
<a name="line32"></a>  
<a name="line33"></a>  /**
<a name="line34"></a>   * Parse module name specified in URL (eg. http://domain.ext/module_page_...)
<a name="line35"></a>   */
<a name="line36"></a>  private static function parse_module_name() {
<a name="line37"></a>    $query_string = trim($_SERVER["QUERY_STRING"]); 
<a name="line38"></a>    if (strlen($query_string)==0) {
<a name="line39"></a>      self::$module = config::get_default_module(); 
<a name="line40"></a>      return;
<a name="line41"></a>    }
<a name="line42"></a>    
<a name="line43"></a>    //no page name
<a name="line44"></a>    if (strpos($query_string,"_")===false) {
<a name="line45"></a>      self::$module = $query_string;
<a name="line46"></a>      return;
<a name="line47"></a>    }
<a name="line48"></a>    
<a name="line49"></a>    //with page name
<a name="line50"></a>    $tokens = explode("_",$query_string);
<a name="line51"></a>    self::$module = $tokens[0]; 
<a name="line52"></a>  }
<a name="line53"></a>  
<a name="line54"></a>  /**
<a name="line55"></a>   * Get module name in HTTP request
<a name="line56"></a>   * @return string The module name
<a name="line57"></a>   */
<a name="line58"></a>  public static function get_module_name() {
<a name="line59"></a>    return self::$module;
<a name="line60"></a>  }
<a name="line61"></a>  
<a name="line62"></a>  /**
<a name="line63"></a>   * Parse page name specified in URL (eg. http://domain.ext/module_page_...)
<a name="line64"></a>   */
<a name="line65"></a>  private static function parse_page_name() {
<a name="line66"></a>    $module = self::get_module_name(); 
<a name="line67"></a>    $module_xml = "modules/$module.xml";
<a name="line68"></a>    if (!file_exists($module_xml)) {
<a name="line69"></a>      response::error("'$module_xml' not found");
<a name="line70"></a>      exit;
<a name="line71"></a>    }
<a name="line72"></a>    
<a name="line73"></a>    //load xml
<a name="line74"></a>    $module_info = xml::arr_from_file($module_xml); 
<a name="line75"></a>    
<a name="line76"></a>    //no query string
<a name="line77"></a>    $query_string = trim($_SERVER["QUERY_STRING"]);    
<a name="line78"></a>    if (strlen($query_string)==0) {
<a name="line79"></a>      self::$page = utils::get_arr_value($module_info,"page/default");    
<a name="line80"></a>      return;
<a name="line81"></a>    }
<a name="line82"></a>      
<a name="line83"></a>    //no page name
<a name="line84"></a>    if (strpos($query_string,"_")===false) {
<a name="line85"></a>      self::$page = utils::get_arr_value($module_info,"page/default");
<a name="line86"></a>      return;
<a name="line87"></a>    }
<a name="line88"></a>      
<a name="line89"></a>    //with page name
<a name="line90"></a>    $tokens = explode("_",$query_string); 
<a name="line91"></a>    self::$page = $tokens[1];
<a name="line92"></a>  }  
<a name="line93"></a> 
<a name="line94"></a>  /**
<a name="line95"></a>   * Get page name specified in URL
<a name="line96"></a>   * @return string The page name
<a name="line97"></a>   */
<a name="line98"></a>  public static function get_page_name() {
<a name="line99"></a>    return self::$page;
<a name="line100"></a>  }
<a name="line101"></a> 
<a name="line102"></a>  /**
<a name="line103"></a>   * Make validated URL based on request URL
<a name="line104"></a>   * @return string The validated url (meaning correct format)
<a name="line105"></a>   */
<a name="line106"></a>  private static function make_validated_url() {
<a name="line107"></a>    $module = self::get_module_name();
<a name="line108"></a>    $page = self::get_page_name();
<a name="line109"></a>    
<a name="line110"></a>    //no query string
<a name="line111"></a>    $query_string = trim($_SERVER["QUERY_STRING"]);
<a name="line112"></a>    if (strlen($query_string)==0) {
<a name="line113"></a>      self::$validated_url = $module."_".$page;
<a name="line114"></a>      return;
<a name="line115"></a>    }
<a name="line116"></a>      
<a name="line117"></a>    //no page name
<a name="line118"></a>    if (strpos($query_string,"_")===false) {
<a name="line119"></a>      self::$validated_url = $module."_".$page;
<a name="line120"></a>      return;
<a name="line121"></a>    }
<a name="line122"></a>      
<a name="line123"></a>    //with page name
<a name="line124"></a>    $tokens = explode("_",$query_string);
<a name="line125"></a>    $count = floor(count($tokens)/2);
<a name="line126"></a>    
<a name="line127"></a>    //make url
<a name="line128"></a>    $url = $module."_".$page;
<a name="line129"></a>    for ($index=1; $index<$count; $index++)
<a name="line130"></a>      $url .= "_".$tokens[$index*2]."_".$tokens[$index*2+1];
<a name="line131"></a>    
<a name="line132"></a>    self::$validated_url = $url;
<a name="line133"></a>  }
<a name="line134"></a>  
<a name="line135"></a>  /**
<a name="line136"></a>   * Get validated URL after reformatting
<a name="line137"></a>   * @return string The formatted relative URL
<a name="line138"></a>   */
<a name="line139"></a>  public static function get_validated_url() {
<a name="line140"></a>    return self::$validated_url;
<a name="line141"></a>  }
<a name="line142"></a>  
<a name="line143"></a>  /**
<a name="line144"></a>   * Obtain parameters in URL
<a name="line145"></a>   */
<a name="line146"></a>  private static function obtain_url_params() {
<a name="line147"></a>    $tokens = explode("_",self::$validated_url);
<a name="line148"></a>    for ($index=1; $index<count($tokens)/2; $index++)
<a name="line149"></a>      self::$url_params[trim($tokens[$index*2])] = $tokens[$index*2+1];
<a name="line150"></a>  }
<a name="line151"></a>  
<a name="line152"></a>  /**
<a name="line153"></a>   * Get URL parameter
<a name="line154"></a>   * @param string $name The name of parameter in URL
<a name="line155"></a>   * @return string The value of param or 'null' if no such param
<a name="line156"></a>   */
<a name="line157"></a>  public static function get_url_var($name) {
<a name="line158"></a>    return isset(self::$url_params[$name])? self::$url_params[$name]:null;
<a name="line159"></a>  }
<a name="line160"></a>  
<a name="line161"></a>  /**
<a name="line162"></a>   * Get form parameter
<a name="line163"></a>   * @param string $name The name of parameter in form (by POST)
<a name="line164"></a>   * @return string The value of param or 'null' if no such param
<a name="line165"></a>   */
<a name="line166"></a>  public static function get_form_var($name) {
<a name="line167"></a>    return isset($_POST[$name])? $_POST[$name]:null;
<a name="line168"></a>  }
<a name="line169"></a>  
<a name="line170"></a>  /**
<a name="line171"></a>   * Get HTTP request method
<a name="line172"></a>   * @return string The name of method, either 'post', or 'get',...
<a name="line173"></a>   */
<a name="line174"></a>  public static function http_method() {
<a name="line175"></a>    return strtolower($_SERVER["REQUEST_METHOD"]);
<a name="line176"></a>  }
<a name="line177"></a>  
<a name="line178"></a>  /**
<a name="line179"></a>   * Check either is HTTP POST method
<a name="line180"></a>   * @return boolean 'true' if is HTTP POST, 'false' otherwise
<a name="line181"></a>   */
<a name="line182"></a>  public static function is_post() {
<a name="line183"></a>    return self::http_method()=="post";
<a name="line184"></a>  }
<a name="line185"></a>
<a name="line186"></a>  /**
<a name="line187"></a>   * Check either is HTTP GET method
<a name="line188"></a>   * @return boolean 'true' if is HTTP GET, 'false' otherwise
<a name="line189"></a>   */
<a name="line190"></a>  public static function is_get() {
<a name="line191"></a>    return self::http_method()=="get";
<a name="line192"></a>  }
<a name="line193"></a>  
<a name="line194"></a>  /**
<a name="line195"></a>   * Get query string
<a name="line196"></a>   * @return string The value of query string in URL
<a name="line197"></a>   */
<a name="line198"></a>  public static function get_query_string() {
<a name="line199"></a>    return $_SERVER["QUERY_STRING"];
<a name="line200"></a>  }
<a name="line201"></a>  
<a name="line202"></a>  /**
<a name="line203"></a>   * Initialise & parse HTTP request
<a name="line204"></a>   */
<a name="line205"></a>  public static function initialise() {  
<a name="line206"></a>    self::parse_module_name(); 
<a name="line207"></a>    self::parse_page_name(); 
<a name="line208"></a>    self::make_validated_url();
<a name="line209"></a>    self::obtain_url_params();
<a name="line210"></a>  }
<a name="line211"></a>}</pre>
<div class="header">
<h1>Cornice</h1>
<ul>
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li><li>Class</li><li>Tree</li><li><a href="../overview-files.html">Files</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li><a href="../todo-list.html">Todo</a></li>
<li><a href="../index-all.html">Index</a></li>
</ul>
</div>

<div class="small_links">
<a href="../index.html" target="_top">Frames</a>
<a href="../source\request.php.html" target="_top">No frames</a>
</div>
<hr>

<p id="footer">This document was generated by <a href="http://peej.github.com/phpdoctor/">PHPDoctor: The PHP Documentation Creator</a></p>

</body>

</html>